//
//  ForgetPassWordTwoVC.swift
//  甜言蜜语
//
//  Created by yingli_yanfa_liuxingyu on 16/9/19.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
import SVProgressHUD
class ForgetPassWordTwoVC: UIViewController {
    var phoneNum: String!
    @IBOutlet weak var backView: UIView!
    @IBOutlet weak var identifyCode: UITextField!
    @IBOutlet weak var newPassWord: UITextField!
    @IBOutlet weak var smsBtn: UIButton!
    @IBOutlet weak var compleBtn: UIButton!
    
    var timeTick: Int = 0
    var timer: NSTimer?
    
    var msg: regiestInfo?
    override func viewDidLoad() {
        super.viewDidLoad()
        publicMethod.setHUDStyle()
        self.backView.layer.masksToBounds = true
        self.backView.layer.cornerRadius = 20
        self.backView.layer.borderWidth = 1
        self.backView.layer.borderColor = NAVIGATIONBAR_RED_COLOR.CGColor
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.tabBarController?.tabBar.hidden = true
         self.timer?.fireDate = NSDate.distantPast()
    }
       
    override func viewDidDisappear(animated: Bool) {
        self.timer?.fireDate = NSDate.distantFuture()
        
        super.viewDidDisappear(animated)
    }
    override func viewWillDisappear(animated: Bool) {
        SVProgressHUD.dismiss()
        super.viewWillDisappear(animated)
    }
    func sendIdfentiCode() {
        self.smsBtn.backgroundColor = UIColor.lightGrayColor()
        timeTick = 61
        //60秒倒计时
        self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(self.timeFireMethod), userInfo: nil, repeats: true)
        self.smsBtn.enabled = false
    }
    
    func timeFireMethod() {
        timeTick -= 1
        if timeTick == 0 {
            timer!.invalidate()
            //停止
            //self.timer = 0
            self.smsBtn.enabled = true
            self.smsBtn.setTitle("重新获取", forState: .Normal)
            self.smsBtn.backgroundColor = UIColor.lightGrayColor()
        }
        else {
            let str: String = "\(timeTick)"
            self.smsBtn.setTitle("\(str)秒", forState: .Normal)
        }
    }
    
    @IBAction func smsBtnClicked(sender: UIButton){
        if self.phoneNum == ""{
            SVProgressHUD.showErrorWithStatus("请输入手机号码")
           
            return
        }else{
            DataTool.modifyPassWordSms(self.phoneNum, completionHandler:{ (newModel) in
                guard newModel != nil else {
                return
                }
                self.msg = newModel
                if self.msg?.msg == "发送成功"{
                    SVProgressHUD.showSuccessWithStatus(self.msg?.msg)
                   
                     self.sendIdfentiCode()
                    
                }else{
                    SVProgressHUD.showErrorWithStatus(self.msg?.msg)
                   
                }
         })


        }
        
    }
    
    @IBAction func compleBtnClicked(sender: UIButton){
        if self.identifyCode == ""{
            SVProgressHUD.showErrorWithStatus("请输入验证码")
           
            return
        }
        if self.newPassWord.text == ""{
             SVProgressHUD.showErrorWithStatus("请输入新密码")
            
            return
        }else{
            DataTool.modifyPassWordComple(self.identifyCode.text!, pass: self.newPassWord.text!, completionHandler:{ (newModel) in
                guard newModel != nil else {
                    return
                }
                self.msg = newModel
                if self.msg?.msg == "修改成功"{
                    SVProgressHUD.showSuccessWithStatus(self.msg?.msg)
                   
                    self.navigationController?.popToRootViewControllerAnimated(true)
                    
                }else{
                    SVProgressHUD.showErrorWithStatus(self.msg?.msg)
                   
                }
            })

        }
        
    }


}
